package com.example.back.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.back.common.Result;
import com.example.back.entity.ChangePassword;
import com.example.back.entity.User;
import com.example.back.exception.ServiceException;
import com.example.back.service.UserService;
import com.example.back.utils.TokenUtils;
import org.apache.logging.log4j.message.ReusableMessage;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Jm
 * @date 2024/7/13
 */
@RestController
@RequestMapping("/user")
public class UserController
{
    @Resource
    UserService userService;

    @GetMapping("/list")
    public Result test()
    {
        Object data = userService.selectAll();
        return Result.success(data);
    }

    @PostMapping("/update")
    public Result update(@RequestBody User user)
    {
        userService.saveOrUpdate(user);
        return Result.success();
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user)
    {
        if(StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword()))
        {
            return Result.error("输入数据不合法");
        }
        User user1 = userService.login(user);
        return Result.success(user1);
    }

    @PostMapping("/changePassword")
    public Result changePassword(@RequestBody ChangePassword changePassword)
    {
        if(changePassword.getId() == null || StrUtil.isBlank(changePassword.getNewPassword()))
        {
            return Result.error("输入数据不合法");
        }
        return Result.success(userService.changePassword(changePassword));
    }


    @GetMapping("/selectPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String username,
                               @RequestParam String name)
    {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.like(StrUtil.isNotBlank(username), "username", username);
        queryWrapper.like(StrUtil.isNotBlank(name), "name", name);

        Page<User> page = userService.page(new Page<>(pageNum, pageSize), queryWrapper);

        return Result.success(page);
    }

    @GetMapping("/selectSchool")
    public Result selectSchool(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam Integer identity)
    {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        if(identity == 0)
        {
            queryWrapper.eq("identity", 3).or().eq("identity",4);
        }else
        {
            queryWrapper.eq("identity", identity);
        }

        Page<User> page = userService.page(new Page<>(pageNum, pageSize), queryWrapper);

        return Result.success(page);
    }

    @GetMapping("/add")
    public Result addUser(@RequestParam Integer userNum,
                          @RequestParam Integer value)
    {
        userService.addUsers(value, userNum);
        return Result.success();
    }

    /**
     * 删除用户信息
     */
//    @HoneyLogs(operation = "用户", type = LogType.DELETE)
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id)
    {
        User currentUser = TokenUtils.getCurrentUser();
        assert currentUser != null;
        if(id.equals(currentUser.getId()))
        {
            throw new ServiceException("不能删除当前的用户");
        }
        userService.removeById(id);
        return Result.success();
    }


    /**
     * 批量删除用户信息
     */
//    @HoneyLogs(operation = "用户", type = LogType.BATCH_DELETE)
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids)
    {  //  [7, 8]
        User currentUser = TokenUtils.getCurrentUser();
        userService.removeBatchByIds(ids);
        return Result.success();
    }

}
